<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\TenantOrder\Service;

use Hyperf\Database\Model\Builder;
use MoChat\App\Tenant\Model\Tenant;
use MoChat\Framework\Service\AbstractService;
use MoChat\Plugin\TenantOrder\Contract\TenantOrderContract;
use MoChat\Plugin\TenantOrder\Model\TenantOrder;
use MoChat\Plugin\TenantOrder\Model\TenantOrderAdjustment;
use MoChat\Plugin\TenantOrder\Model\TenantOrderItem;
use MoChat\Plugin\TenantOrder\Model\TenantOrderItemUnit;
use MoChat\Plugin\TenantOrder\Model\TenantOrderPayment;
use MoChat\Plugin\TenantOrder\Model\TenantOrderShipment;

class TenantOrderService extends AbstractService implements TenantOrderContract
{
    /**
     * @var TenantOrder
     */
    protected $model;

    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantOrderById(int $id, array $columns = ['*']): array
    {
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 查询多条 - 根据ID.
     * @param array $ids ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantOrdersById(array $ids, array $columns = ['*']): array
    {
        return $this->model->getAllById($ids, $columns);
    }

    /**
     * 多条分页.
     * @param array $where 查询条件
     * @param array|string[] $columns 查询字段
     * @param array $options 可选项 ['orderByRaw'=> 'id asc', 'perPage' => 15, 'page' => null, 'pageName' => 'page']
     * @return array 分页结果 Hyperf\Paginator\Paginator::toArray
     */
    public function getTenantOrderList(array $where, array $columns = ['*'], array $options = []): array
    {
        return $this->model->getPageList($where, $columns, $options);
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createTenantOrder(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 添加多条
     * @param array $data 添加的数据
     * @return bool 执行结果
     */
    public function createTenantOrders(array $data): bool
    {
        return $this->model->createAll($data);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateTenantOrderById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 删除 - 单条
     * @param int $id 删除ID
     * @return int 删除条数
     */
    public function deleteTenantOrder(int $id): int
    {
        return $this->model->deleteOne($id);
    }

    /**
     * 删除 - 多条
     * @param array $ids 删除ID
     * @return int 删除条数
     */
    public function deleteTenantOrders(array $ids): int
    {
        return $this->model->deleteAll($ids);
    }

    /**
     * 查询一条
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantOrderByTenantIdSkuId(int $tenantId, int $skuId, array $columns = ['*']): array
    {
        $res = $this->model::from($this->model::query()->getModel()->getTable() . ' as tenant_order')
            ->join(TenantOrderItem::query()->getModel()->getTable() . ' as tenant_order_item', 'tenant_order.id', 'tenant_order_item.order_id')
            ->where('tenant_order.tenant_id', $tenantId)
            ->where('tenant_order_item.sku_id', $skuId)
            ->orderByDesc('tenant_order.id')
            ->first(['tenant_order.total']);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 添加单条-item.
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createTenantOrderItem(array $data): int
    {
        return (new TenantOrderItem())->createOne($data);
    }

    /**
     * 添加单条-shipment.
     */
    public function createTenantOrderShipment(array $data): int
    {
        return (new TenantOrderShipment())->createOne($data);
    }

    /**
     * 添加单条-itemUnit.
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createTenantOrderItemUnit(array $data): int
    {
        return (new TenantOrderItemUnit())->createOne($data);
    }

    /**
     * 添加单条-adjustment.
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createTenantOrderAdjustment(array $data): int
    {
        return (new TenantOrderAdjustment())->createOne($data);
    }

    /**
     * 添加单条-payment.
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createTenantOrderPayment(array $data): int
    {
        return (new TenantOrderPayment())->createOne($data);
    }

    /**
     * 查询多条
     */
    public function getTenantOrderListBySearch(int $ownerId, string $phone, array $status, string $startTime, string $endTime): array
    {
        $res = $this->model::from($this->model::query()->getModel()->getTable() . ' as tenant_order')
            ->join(TenantOrderItem::query()->getModel()->getTable() . ' as tenant_order_item', 'tenant_order.id', 'tenant_order_item.order_id')
            ->join(Tenant::query()->getModel()->getTable() . ' as tenant', 'tenant_order.tenant_id', 'tenant.id')
            ->when($ownerId > 0, function (Builder $query) use ($ownerId) {
                return $query->where('tenant.owner_id', $ownerId);
            })
            ->when(! empty($phone), function (Builder $query) use ($phone) {
                return $query->where('tenant.name', 'like', "%{$phone}%")
                    ->orWhere('tenant.phone', 'like', "%{$phone}%");
            })
            ->when(! empty($status), function (Builder $query) use ($status) {
                return $query->whereIn('tenant_order.status', $status);
            })
            ->when(! empty($startTime), function (Builder $query) use ($startTime, $endTime) {
                return $query->whereBetween('tenant_order.created_at', [$startTime, $endTime]);
            })
            ->orderByDesc('tenant_order.id')
            ->get(['tenant_order.id', 'tenant_order.user_id', 'tenant_order.tenant_id', 'tenant_order.total', 'tenant_order.created_at', 'tenant_order.status', 'tenant_order_item.sku_id']);
        $res || $res = collect([]);
        return $res->toArray();
    }
}
